package com.company.cloud.user.modules.app.controller;

import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseReturnEnum;

import com.company.cloud.user.config.UserCenterConfig;
import com.company.common.server.config.cloud.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.constant.ErrorUserEnum;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.constant.UserStateEnum;
import com.company.cloud.user.modules.app.controller.req.UserForgetSendVCodeParam;
import com.company.cloud.user.modules.app.controller.req.UserForgetUpdatePasswordParam;
import com.company.cloud.user.modules.app.controller.req.UserForgetValidateCodeParam;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.service.UserInfoService;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("/app/user/forget")
@Api(tags="User_App_用户忘记密码服务")
public class UserForgetController {

	@Autowired
	private  UserInfoService userInfoService;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private UserCodeService userCodeService;

//	@Autowired
//	private MessageSMSRemoteServiceImpl messageSMSRemoteServiceImpl;
	@Autowired
	private ProjectConfig projectConfig;
	@Autowired
	private UserCenterConfig userCenterConfig;




	@GetMapping("/forgetPDMobile")
	@ApiOperation(value="验证手机号(忘记密码)")
	public BaseResult forgetPDMobile(@RequestParam("mobile")String mobile) {
		String companyNo=projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
		if(StrUtil.hasEmpty(mobile,companyNo)){
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		UserInfo user= userInfoService.getOne(new QueryWrapper<UserInfo>()
					.eq("mobile", mobile)
					.eq(projectConfig.isCompanyPattern(), "company_no", companyNo));
		if(user==null) {
			throw new BusinessException(ErrorUserEnum.User_Info_Null);
		}
		if(!UserStateEnum.NORMAL.getCode().equals(user.getUserState())) {
			throw new BusinessException(ErrorUserEnum.User_Info_Null);
		}
		String key=StrUtil.format("UFP_M_{}", IdUtil.fastSimpleUUID());//验证成功KEY
		redisUtils.set(key, user.getId(), 300); //5分钟
		return BaseResult.ok(key);
	}
	
	@GetMapping("/sendForgetPDVcode")
	@ApiOperation(value="发送验证码(忘记密码)")
	public BaseResult sendForgetPDVcode(UserForgetSendVCodeParam params) {
		ValidatorUtils.validate(params);
		String companyNo=projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
		if(StrUtil.isEmpty(companyNo)){
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		String userId= redisUtils.get(params.getKey());
		if(StringUtils.isEmpty(userId)) {
			throw new BusinessException(ErrorUserEnum.User_Forget_Op_Error);
		}
		userCodeService.sendCodeByCompany(companyNo, UserMsgSendTypeEnum.Third_Bind_VCode, params.getMobile());
		redisUtils.delete(params.getKey());
		return BaseResult.ok();
	}
	
	
	@GetMapping("/validateForgetPDVcode")
	@ApiOperation(value="验证验证码(忘记密码)")
	public BaseResult validateForgetPDVcode(UserForgetValidateCodeParam params) {
		ValidatorUtils.validate(params);
		String companyNo=projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
		if(StrUtil.isEmpty(companyNo)){
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		userCodeService.validate(companyNo, UserMsgSendTypeEnum.Third_Bind_VCode, params.getMobile(), params.getVcode(), false);
		String key=IdUtil.fastSimpleUUID();
		redisUtils.set(key, params.getMobile(), 60*5); //5分钟
		return BaseResult.ok(key);
	}
	
	@PostMapping("/updateForgetPassword")
	@ApiOperation(value="修改密码(忘记密码)")
	public BaseResult updateForgetPassword(@RequestBody UserForgetUpdatePasswordParam params) {
		ValidatorUtils.validate(params);
		String userMobile= redisUtils.get(params.getKey());
		if(StringUtils.isEmpty(userMobile)) {
//			throw new BusinessException(UserErrorEnum.User_Forget_OpError);
		}
//		String[] passwords= PasswordUtils.getMd5Password(params.getPassword());
//		UserInfo user=new UserInfo();
//		user.setId(Integer.valueOf(userId));
//		user.setSalt(passwords[1]);
//		user.setPassword(passwords[0]);
//		user.setEdt(LocalDateTime.now());
//		user.setEip(HttpContextUtils.getClientIP());
//		userInfoService.updateById(user);
		return BaseResult.ok();
	}
	
	
}
